package com.rovinj_server.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.rovinj_server.R;
import com.rovinj_server.db.DBManager;
import com.rovinj_server.entity.InventoryTask;
import com.rovinj_server.entity.InventoryTaskSub;
import com.rovinj_server.entity.SubInfo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;

public class KFDActivity extends Activity {

    Socket socket = null;
    String buffer = "";
    TextView txt1;
    Button send;
    EditText ed1;
    String geted1;

    Button but1, but2, but3, but4, but5, but6, but7;
    public List<InventoryTask> mTaskList = new ArrayList<>();
    public Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 1:
                    Bundle bundle = msg.getData();
                    mMStr = bundle.getString("msg");
                    System.out.println("---"+mMStr);
                    break;
            }
            if (msg.what == 66) {
            /*    Bundle bundle = msg.getData();
                txt1.append("myHandler 服务端返回信息：" + bundle.getString("msg") + "\n");
                mMStr = bundle.getString("msg");
                Gson gson = new Gson();
                SubInfo subInfo = JSON.parseObject(mMStr, SubInfo.class);
                List<InventoryTask> taskList = subInfo.getTaskList();
                List<InventoryTaskSub> taskSubList = subInfo.getTaskSubList();
                System.out.println("---"+taskList.size());
                System.out.println("---"+taskSubList.size());*/
            }
        }
    };
    @Bind(R.id.ed1)
    EditText mEd1;
    private String mMStr;
    private DBManager mDbManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kfd);
        ButterKnife.bind(this);
        txt1 = (TextView) findViewById(R.id.txt1);
        send = (Button) findViewById(R.id.send);
        ed1 = (EditText) findViewById(R.id.ed1);
        mDbManager = new DBManager(this);
        mDbManager.copyDBFile();
        new MyThread("建立连接").start();
        send.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 启动线程 向服务器发送和接收信息
                new MyThread("dsa").start();
            }
        });
    }


    class MyThread extends Thread {
        public String txt1;

        public MyThread(String str) {
            txt1 = str;
        }

        @Override
        public void run() {
            // 定义消息
            List<InventoryTaskSub> taskSubList = mDbManager.findTaskSubList();
            Map map = new HashMap();
            map.put("state", "1");
            map.put("mTaskSubList", taskSubList);
            final String postData = JSON.toJSONString(map);
            final String str = postData;
            Message msg = new Message();
            msg.what = 1;
            Bundle bundle = new Bundle();
            bundle.clear();
            try {
                // 连接服务器 并设置连接超时为5秒
                socket = new Socket();
                socket.connect(new InetSocketAddress("192.168.31.239", 30000), 1000);
                // 获取输入输出流

                BufferedReader bff = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                // 读取发来服务器信息
                String line = null;
                buffer = "";
                while ((line = bff.readLine()) != null) {
                    buffer = line + buffer;
                }
                OutputStream ou = socket.getOutputStream();
                // 向服务器发送信息
                ou.write(str.getBytes("UTF-8"));
                ou.flush();
                bundle.putString("msg", buffer.toString());
                msg.setData(bundle);
                //  发送消息 修改UI线程中的组件
                myHandler.sendMessage(msg);
                // 关闭各种输入输出流
                bff.close();
                ou.close();

                socket.close();
            } catch (SocketTimeoutException aa) {
                // 连接超时 在UI界面显示消息
                bundle.putString("msg", "失败=======");
                msg.setData(bundle);
                // 发送消息 修改UI线程中的组件
                myHandler.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}